diff options
author | Denis Dzyubenko <denis.dzyubenko@nokia.com> | 2012-03-29 14:50:27 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-03-30 13:43:58 +0200 |
commit | 6ca106f2baeebcaf532b552c8871d1dba687107a (patch) | |
tree | 53568d7547d0069ed8225f600111d3ed4d7142f4 | |
parent | dfedef7e9ba8fdb39e4faa7aed6e30fce4a79447 (diff) |
Removed duplicate jsonstream class.
This creates a static library that handles the wire protocol, which is reused
by both server side and client side.
Change-Id: Ic90af9a73430085cf7132bdef42a80898a82e391
Reviewed-by: Kevin Simons <kevin.simons@nokia.com>
-rw-r--r-- | src/client/qjsondbconnection.cpp | 2 | ||||
-rw-r--r-- | src/client/qjsondbconnection_p.h | 2 | ||||
-rw-r--r-- | src/clientcompat/clientcompat.pro | 1 | ||||
-rw-r--r-- | src/clientcompat/jsondb-connection.cpp | 1 | ||||
-rw-r--r-- | src/clientcompat/jsondb-connection_p_p.h | 4 | ||||
-rw-r--r-- | src/common/common.pri | 6 | ||||
-rw-r--r-- | src/common/jsonstream.cpp | 141 | ||||
-rw-r--r-- | src/common/jsonstream.h | 89 | ||||
-rw-r--r-- | src/daemon/daemon.pri | 1 | ||||
-rw-r--r-- | src/daemon/dbserver.cpp | 6 | ||||
-rw-r--r-- | src/daemon/dbserver.h | 2 | ||||
-rw-r--r-- | src/jsonstream/jsonstream.cpp | 4 | ||||
-rw-r--r-- | src/jsonstream/jsonstream.h | 15 | ||||
-rw-r--r-- | src/jsonstream/jsonstream.pri | 3 | ||||
-rw-r--r-- | src/jsonstream/jsonstream.pro | 3 | ||||
-rw-r--r-- | tests/auto/jsonstream/jsonstream.pro | 2 | ||||
-rw-r--r-- | tests/auto/jsonstream/test-jsonstream.cpp | 15 |
17 files changed, 43 insertions, 254 deletions
diff --git a/src/client/qjsondbconnection.cpp b/src/client/qjsondbconnection.cpp index f7f10b12..ba370572 100644 --- a/src/client/qjsondbconnection.cpp +++ b/src/client/qjsondbconnection.cpp @@ -162,7 +162,7 @@ QJsonDbConnectionPrivate::QJsonDbConnectionPrivate(QJsonDbConnection *q) QObject::connect(socket, SIGNAL(connected()), q_ptr, SLOT(_q_onConnected())); QObject::connect(socket, SIGNAL(error(QLocalSocket::LocalSocketError)), q_ptr, SLOT(_q_onError(QLocalSocket::LocalSocketError))); - stream = new JsonStream::JsonStream(q); + stream = new QtJsonDbJsonStream::JsonStream(q); stream->setDevice(socket); QObject::connect(stream, SIGNAL(receive(QJsonObject)), q_ptr, SLOT(_q_onReceivedObject(QJsonObject))); diff --git a/src/client/qjsondbconnection_p.h b/src/client/qjsondbconnection_p.h index bf65e6b3..11199fd1 100644 --- a/src/client/qjsondbconnection_p.h +++ b/src/client/qjsondbconnection_p.h @@ -103,7 +103,7 @@ public: QTimer timeoutTimer; QLocalSocket *socket; - JsonStream::JsonStream *stream; + QtJsonDbJsonStream::JsonStream *stream; int lastRequestId; QWeakPointer<QJsonDbRequest> currentRequest; diff --git a/src/clientcompat/clientcompat.pro b/src/clientcompat/clientcompat.pro index 38b03f88..c5b8f05b 100644 --- a/src/clientcompat/clientcompat.pro +++ b/src/clientcompat/clientcompat.pro @@ -15,6 +15,7 @@ CONFIG += module create_prl MODULE_PRI = ../../modules/qt_jsondbcompat.pri include(../common/common.pri) +include(../jsonstream/jsonstream.pri) HEADERS += qtjsondbcompatversion.h diff --git a/src/clientcompat/jsondb-connection.cpp b/src/clientcompat/jsondb-connection.cpp index 9e1d0f1b..78899df5 100644 --- a/src/clientcompat/jsondb-connection.cpp +++ b/src/clientcompat/jsondb-connection.cpp @@ -46,6 +46,7 @@ #include "jsondb-connection_p_p.h" #include "qjsonobject.h" +#include "qjsonarray.h" QT_BEGIN_NAMESPACE_JSONDB diff --git a/src/clientcompat/jsondb-connection_p_p.h b/src/clientcompat/jsondb-connection_p_p.h index 3eb06cdb..ab426a6f 100644 --- a/src/clientcompat/jsondb-connection_p_p.h +++ b/src/clientcompat/jsondb-connection_p_p.h @@ -64,7 +64,7 @@ class JsonDbConnectionPrivate Q_DECLARE_PUBLIC(JsonDbConnection) public: JsonDbConnectionPrivate(JsonDbConnection *q) - : q_ptr(q), socket(0), tcpSocket(0), mStream(0, q), mId(1), status(JsonDbConnection::Null) + : q_ptr(q), socket(0), tcpSocket(0), mStream(q), mId(1), status(JsonDbConnection::Null) { } ~JsonDbConnectionPrivate() { } @@ -77,7 +77,7 @@ public: JsonDbConnection *q_ptr; QLocalSocket *socket; QTcpSocket *tcpSocket; - JsonStream mStream; + QtJsonDbJsonStream::JsonStream mStream; int mId; JsonDbConnection::Status status; QString errorString; diff --git a/src/common/common.pri b/src/common/common.pri index 01e012ca..8a5190af 100644 --- a/src/common/common.pri +++ b/src/common/common.pri @@ -15,10 +15,8 @@ HEADERS += \ $$PWD/jsondb-error.h \ $$PWD/jsondb-strings.h \ $$PWD/jsondbcollator.h \ - $$PWD/jsondbcollator_p.h \ - $$PWD/jsonstream.h + $$PWD/jsondbcollator_p.h SOURCES += \ $$PWD/jsondb-strings.cpp \ - $$PWD/jsondbcollator.cpp \ - $$PWD/jsonstream.cpp + $$PWD/jsondbcollator.cpp diff --git a/src/common/jsonstream.cpp b/src/common/jsonstream.cpp deleted file mode 100644 index c2500eff..00000000 --- a/src/common/jsonstream.cpp +++ /dev/null @@ -1,141 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/ -** -** This file is part of the QtAddOn.JsonDb module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** GNU Lesser General Public License Usage -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this -** file. Please review the following information to ensure the GNU Lesser -** General Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU General -** Public License version 3.0 as published by the Free Software Foundation -** and appearing in the file LICENSE.GPL included in the packaging of this -** file. Please review the following information to ensure the GNU General -** Public License version 3.0 requirements will be met: -** http://www.gnu.org/copyleft/gpl.html. -** -** Other Usage -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** -** -** -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "jsonstream.h" -#include <QDebug> -#include <QDataStream> -#include <QLocalSocket> -#include <QAbstractSocket> -#include <QtEndian> - -#ifdef QT_DEBUG -#include <QUdpSocket> -#include <QDateTime> -#include <QCoreApplication> -#endif - -QT_BEGIN_NAMESPACE_JSONDB - -JsonStream::JsonStream(QIODevice *device, QObject *parent) : - QObject(parent), - mDevice(0) -{ - setDevice(device); -} - -QIODevice *JsonStream::device() const -{ - return mDevice; -} - -/** Set the device used by the JsonStream. - The stream does not take ownership of the device. -*/ -void JsonStream::setDevice(QIODevice *device) -{ - if (mDevice) { - disconnect(mDevice, SIGNAL(readyRead()), this, SLOT(deviceReadyRead())); - disconnect(mDevice, SIGNAL(bytesWritten(qint64)), this, SLOT(deviceBytesWritten(qint64))); - disconnect(mDevice, SIGNAL(aboutToClose()), this, SIGNAL(aboutToClose())); - } - mDevice = device; - if (mDevice) { - connect(mDevice, SIGNAL(readyRead()), this, SLOT(deviceReadyRead())); - connect(mDevice, SIGNAL(bytesWritten(qint64)), this, SLOT(deviceBytesWritten(qint64))); - connect(mDevice, SIGNAL(aboutToClose()), this, SIGNAL(aboutToClose())); - } -} - -bool JsonStream::send(const QJsonObject &object) -{ - QByteArray data = QJsonDocument(object).toBinaryData(); - int shouldWrite = data.size(); - if (mWriteBuffer.isEmpty()) { - int didWrite = mDevice->write(data); - if (didWrite < 0) { - qWarning() << "Error writing to socket" << mDevice->errorString(); - } else if (didWrite < shouldWrite) { - mWriteBuffer = data.mid(didWrite); - } - QLocalSocket *s = qobject_cast<QLocalSocket *>(mDevice); - if (s) - s->flush(); - } else { - qWarning() << "Buffering, slow down your writes"; - mWriteBuffer.append(data); - } - return true; -} - -void JsonStream::deviceReadyRead() -{ - while (!mDevice->atEnd()) { - int bytesAvailable = mDevice->bytesAvailable(); - int offset = mReadBuffer.size(); - mReadBuffer.resize(offset + bytesAvailable); - int bytesRead = mDevice->read(mReadBuffer.data()+offset, bytesAvailable); - if (bytesRead < 0) { - qWarning() << "Error reading from socket" << mDevice->errorString(); - continue; - } - while (mReadBuffer.size()) { - QJsonDocument doc(QJsonDocument::fromBinaryData(mReadBuffer, QJsonDocument::Validate)); - if (doc.isEmpty()) - break; - receive(doc.object()); - mReadBuffer = mReadBuffer.mid(doc.toBinaryData().size()); - } - } -} - -void JsonStream::deviceBytesWritten(qint64 bytes) -{ - Q_UNUSED(bytes); - if (!mWriteBuffer.isEmpty()) { - int didWrite = mDevice->write(mWriteBuffer); - mWriteBuffer = mWriteBuffer.mid(didWrite); - } -} - - -#include "moc_jsonstream.cpp" - -QT_END_NAMESPACE_JSONDB diff --git a/src/common/jsonstream.h b/src/common/jsonstream.h deleted file mode 100644 index eab8bda0..00000000 --- a/src/common/jsonstream.h +++ /dev/null @@ -1,89 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies). -** Contact: http://www.qt-project.org/ -** -** This file is part of the QtAddOn.JsonDb module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** GNU Lesser General Public License Usage -** This file may be used under the terms of the GNU Lesser General Public -** License version 2.1 as published by the Free Software Foundation and -** appearing in the file LICENSE.LGPL included in the packaging of this -** file. Please review the following information to ensure the GNU Lesser -** General Public License version 2.1 requirements will be met: -** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -** -** In addition, as a special exception, Nokia gives you certain additional -** rights. These rights are described in the Nokia Qt LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU General -** Public License version 3.0 as published by the Free Software Foundation -** and appearing in the file LICENSE.GPL included in the packaging of this -** file. Please review the following information to ensure the GNU General -** Public License version 3.0 requirements will be met: -** http://www.gnu.org/copyleft/gpl.html. -** -** Other Usage -** Alternatively, this file may be used in accordance with the terms and -** conditions contained in a signed written agreement between you and Nokia. -** -** -** -** -** -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef JSON_STREAM_H -#define JSON_STREAM_H - -#include <QIODevice> -#include <qjsondocument.h> -#include <qjsonarray.h> -#include <qjsonobject.h> -#include <qjsonvalue.h> - -#include "jsondb-global.h" - -QT_BEGIN_HEADER - -QT_BEGIN_NAMESPACE_JSONDB - -class JsonStream : public QObject -{ - Q_OBJECT -public: - explicit JsonStream(QIODevice *device = 0, QObject *parent = 0); - - QIODevice *device() const; - void setDevice(QIODevice *device); - - bool send(const QJsonObject &document); - -Q_SIGNALS: - void receive(const QJsonObject &data); - void aboutToClose(); - void readyWrite(); - -protected slots: - void deviceReadyRead(); - void deviceBytesWritten(qint64 bytes); - -private: - - QIODevice *mDevice; - QByteArray mWriteBuffer; - QByteArray mReadBuffer; - -}; - -QT_END_NAMESPACE_JSONDB - -QT_END_HEADER - -#endif // JSON_STREAM_H diff --git a/src/daemon/daemon.pri b/src/daemon/daemon.pri index 2952da2f..9e833500 100644 --- a/src/daemon/daemon.pri +++ b/src/daemon/daemon.pri @@ -1,5 +1,6 @@ include($$PWD/../3rdparty/btree/btree.pri) include($$PWD/../common/common.pri) +include($$PWD/../jsonstream/jsonstream.pri) DEFINES += $$quote(QT_BEGIN_MOC_NAMESPACE=\"QT_USE_NAMESPACE QT_USE_NAMESPACE_JSONDB\") diff --git a/src/daemon/dbserver.cpp b/src/daemon/dbserver.cpp index 0c0a31b7..b153a58e 100644 --- a/src/daemon/dbserver.cpp +++ b/src/daemon/dbserver.cpp @@ -305,7 +305,8 @@ void DBServer::handleConnection() if (jsondbSettings->debug()) qDebug() << "client connected to jsondb server" << connection; connect(connection, SIGNAL(disconnected()), this, SLOT(removeConnection())); - JsonStream *stream = new JsonStream(connection, this); + JsonStream *stream = new JsonStream(this); + stream->setDevice(connection); connect(stream, SIGNAL(receive(QJsonObject)), this, SLOT(receiveMessage(QJsonObject))); mConnections.insert(connection, stream); @@ -318,7 +319,8 @@ void DBServer::handleTcpConnection() if (jsondbSettings->debug()) qDebug() << "remote client connected to jsondb server" << connection; connect(connection, SIGNAL(disconnected()), this, SLOT(removeConnection())); - JsonStream *stream = new JsonStream(connection, this); + JsonStream *stream = new JsonStream(this); + stream->setDevice(connection); connect(stream, SIGNAL(receive(QJsonObject)), this, SLOT(receiveMessage(QJsonObject))); mConnections.insert(connection, stream); diff --git a/src/daemon/dbserver.h b/src/daemon/dbserver.h index d4df0cca..159a6f90 100644 --- a/src/daemon/dbserver.h +++ b/src/daemon/dbserver.h @@ -60,6 +60,8 @@ QT_END_NAMESPACE QT_BEGIN_NAMESPACE_JSONDB +using QtJsonDbJsonStream::JsonStream; + class JsonDbEphemeralPartition; class DBServer : public QObject diff --git a/src/jsonstream/jsonstream.cpp b/src/jsonstream/jsonstream.cpp index 70a35992..ce1de0be 100644 --- a/src/jsonstream/jsonstream.cpp +++ b/src/jsonstream/jsonstream.cpp @@ -56,7 +56,7 @@ QT_BEGIN_NAMESPACE -namespace JsonStream { +namespace QtJsonDbJsonStream { JsonStream::JsonStream(QObject *parent) : QObject(parent), mDevice(0) @@ -158,7 +158,7 @@ void JsonStream::deviceBytesWritten(qint64 bytes) } } -} // namespace JsonStream +} // namespace QtJsonDbJsonStream QT_END_NAMESPACE diff --git a/src/jsonstream/jsonstream.h b/src/jsonstream/jsonstream.h index f76020ff..8eceab04 100644 --- a/src/jsonstream/jsonstream.h +++ b/src/jsonstream/jsonstream.h @@ -42,6 +42,17 @@ #ifndef JSON_STREAM_H #define JSON_STREAM_H +// +// W A R N I N G +// ------------- +// +// This file is not part of the QtJsonDb API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + #include <QObject> #include <QJsonObject> @@ -49,7 +60,7 @@ QT_BEGIN_NAMESPACE class QIODevice; -namespace JsonStream { +namespace QtJsonDbJsonStream { class JsonStream : public QObject { @@ -76,7 +87,7 @@ private: QByteArray mReadBuffer; }; -} // namespace JsonStream +} // namespace QtJsonDbJsonStream QT_END_NAMESPACE diff --git a/src/jsonstream/jsonstream.pri b/src/jsonstream/jsonstream.pri index 6bbb9b79..741a53bd 100644 --- a/src/jsonstream/jsonstream.pri +++ b/src/jsonstream/jsonstream.pri @@ -1,2 +1,3 @@ INCLUDEPATH += $$PWD -LIBS_PRIVATE += -L../jsonstream -lqtjsondbjsonstream +LIBS_PRIVATE += -L$$QT_MODULE_LIB_BASE -lQtJsonDbJsonStream +POST_TARGETDEPS += $${QT_MODULE_LIB_BASE}$${QMAKE_DIR_SEP}libQtJsonDbJsonStream.a diff --git a/src/jsonstream/jsonstream.pro b/src/jsonstream/jsonstream.pro index cb7ea280..133974bb 100644 --- a/src/jsonstream/jsonstream.pro +++ b/src/jsonstream/jsonstream.pro @@ -1,6 +1,7 @@ -TARGET = qtjsondbjsonstream +TARGET = QtJsonDbJsonStream TEMPLATE = lib +DESTDIR = $$QT_MODULE_LIB_BASE CONFIG += qt staticlib QT = core network diff --git a/tests/auto/jsonstream/jsonstream.pro b/tests/auto/jsonstream/jsonstream.pro index 172fc7b2..f2f75c2f 100644 --- a/tests/auto/jsonstream/jsonstream.pro +++ b/tests/auto/jsonstream/jsonstream.pro @@ -4,7 +4,7 @@ QT = qml network testlib CONFIG -= app_bundle CONFIG += testcase -include($$PWD/../../../src/common/common.pri) +include($$PWD/../../../src/jsonstream/jsonstream.pri) SOURCES += \ test-jsonstream.cpp diff --git a/tests/auto/jsonstream/test-jsonstream.cpp b/tests/auto/jsonstream/test-jsonstream.cpp index 4eac182e..15639241 100644 --- a/tests/auto/jsonstream/test-jsonstream.cpp +++ b/tests/auto/jsonstream/test-jsonstream.cpp @@ -40,14 +40,15 @@ ****************************************************************************/ #include <QtTest/QtTest> -#include <jsonstream.h> +#include "jsonstream.h" + #include <QLocalServer> #include <QLocalSocket> #include <QBuffer> #include <QDebug> -QT_ADDON_JSONDB_USE_NAMESPACE +using QtJsonDbJsonStream::JsonStream; class TestJsonStream: public QObject { @@ -81,10 +82,9 @@ void TestJsonStream::testJsonStream() QVERIFY(device->waitForConnected()); QVERIFY(device->state() == QLocalSocket::ConnectedState); - JsonStream *stream = new JsonStream(device, this); - - connect(stream, SIGNAL(receive(QJsonObject)), - this, SLOT(receiveStream(QJsonObject))); + JsonStream *stream = new JsonStream(this); + stream->setDevice(device); + connect(stream, SIGNAL(receive(QJsonObject)), this, SLOT(receiveStream(QJsonObject))); qApp->processEvents(); qApp->processEvents(); @@ -99,7 +99,8 @@ void TestJsonStream::handleSocketConnection() { qDebug() << "handleSocketConnection"; serverOk = true; - JsonStream sender(server->nextPendingConnection()); + JsonStream sender; + sender.setDevice(server->nextPendingConnection()); QJsonObject json1; json1.insert("hello", QString("world")); |